<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Puppet学习之文件管理 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Puppet学习之文件管理</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Puppet/">Puppet</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用puppet可以方便的进行文件管理，可以对文件的内容、属组、属主、权限等进行管理。可管理的文件类型包括普通文件、目录、链接文件。类型需要在ensure类型中明确指定。接下来从三个方面来介绍puppet文件管理的操作。</p>
<h2 id="一、Puppet服务器端的配置"><a href="#一、Puppet服务器端的配置" class="headerlink" title="一、Puppet服务器端的配置"></a>一、Puppet服务器端的配置</h2><ol>
<li>fileserver.conf文件的配置</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;vim fileserver.conf 添加如下内容，在puppet master上创建一个文件存放目录</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[files]</div><div class="line">path /etc/puppet/files</div><div class="line">allow 10.1.4.0/24</div></pre></td></tr></table></figure>
<ol>
<li>site.pp文件的配置</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最好是更改其他的文件，然后用这个文件import进来，我这里为了测试简便就直接更改这个文件了，具体的如何操作见后面的实战部分。</p>
<h2 id="二、Puppet文件管理重要参数介绍"><a href="#二、Puppet文件管理重要参数介绍" class="headerlink" title="二、Puppet文件管理重要参数介绍"></a>二、Puppet文件管理重要参数介绍</h2><ol>
<li>backup参数</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定在文件内容替换之前进行备份操作，可以备份在本地，也可以集中备份。集中远程备份的话可以使用filebucket（我们在后面的实战部分会进行详细介绍），这个备份的时候如果备份在本地可以指定备份的文件名。</p>
<ol>
<li>content参数</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定文件的内容(字符串)，这个参数和source、target参数冲突。</p>
<ol>
<li>ensure参数</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个参数指定是否创建、删除文件或者目录，有present、absent、file、directory等值。其中present会检查文件是否存在，不存在就会创建一个空文件。absent会删除文件或者目录，如果是目录需要指定recurse参数指定是否允许递归。如果指定的是其他的参数，则会创建连接文件，为了方便管理，建议在创建的时候使用ensure =&gt; link,并通过target参数指定文件。注意不能在windows系统上链接文件，</p>
<ol>
<li>force参数</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该参数强制执行文件操作，进行如下操作的时候必须指定force参数</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;purge 子目录</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用文件或者链接文件替换目录</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用ensure =&gt; absent参数删除目录</p>
<ol>
<li>group参数</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定文件或者目录的属组，可以是组名或者组id，如果是windows的话属组和属主不能相同。</p>
<ol>
<li>ignore参数</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个参数指定在递归期间对符合指定的模式的文件操作将被忽略。</p>
<ol>
<li>links参数</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个参数指定处理文件期间如何处理链接文件，可以设置follow和manage。在拷贝文件的时候，follow将会拷贝目标文件代替链接文件，manage将只会拷贝链接文件，ignore将会跳过。</p>
<ol>
<li>mode参数</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个参数用来指定文件或者目录的权限，puppet使用传统的unix权限方案，如果系统采用的权限方案不同的，puppet为这些系统将权限翻译成等价的权限，比如windows。这些权限可以是数字(r=4,w=2,x=1)也可以是字符(rwxst)。</p>
<ol>
<li>owner参数</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定文件的属主，可以是用户名或是用户id，如果是windows的话属组和属主不能相同。</p>
<ol>
<li>path参数</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定文件管理的路径。Windows路径也使用/而不是\。</p>
<ol>
<li>purge参数</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个参数会删除在master上不存在的文件，这个参数只有在管理目录的时候指定了recurse =&gt; true参数的时候才有意义。</p>
<ol>
<li>recurse参数</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个参数指定是否进行递归调用以及递归调用的深度，选项如下</p>
<ul>
<li>inf,true  —在远程和本地都进行递归调用</li>
<li>remote —只在远程进行递归调用</li>
<li>false —不进行递归调用</li>
<li>[0-9]+ —和true参数一样，但是限制递归调用目录的深度</li>
</ul>
<ol>
<li>source参数</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该参数指定将会被拷贝到指定位置的资源文件，值可以是指定远程文件的URIS或者本地的完整路径。可以指定多个sorce，这个参数和content、target冲突。</p>
<ol>
<li>target参数</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个参数指定创建链接文件的目标文件或者目录。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面只是列了一些常用的选项，更多选项请参考：</p>
<p><a href="https://docs.puppet.com/puppet/latest/type.html#augeas-attributes" target="_blank" rel="external">https://docs.puppet.com/puppet/latest/type.html#augeas-attributes</a></p>
<h3 id="三、Puppet文件管理实战"><a href="#三、Puppet文件管理实战" class="headerlink" title="三、Puppet文件管理实战"></a>三、Puppet文件管理实战</h3><ol>
<li>使用filebucket进行远程备份。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Filebucket的配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim site.pp</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#添加filebucket的配置</span></div><div class="line">filebucket &#123; <span class="string">'main'</span>:</div><div class="line">    server =&gt; <span class="string">"puppet.zhang.com"</span>,</div><div class="line">    path =&gt; <span class="literal">false</span>,</div><div class="line">&#125;</div><div class="line"><span class="comment">#让节点node1.zhang.com进行远程备份</span></div><div class="line">node <span class="string">'node1.zhang.com'</span>&#123;</div><div class="line">file &#123; <span class="string">"/etc/test2"</span>:</div><div class="line">    <span class="built_in">source</span> =&gt; <span class="string">"puppet://puppet.zhang.com/files/test2"</span>,</div><div class="line">    backup =&gt; main</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果没有设置bucketdir路径，那么默认文件保存在master的$vardir/bucket路径。存储是以文件的md5值为目录，目录下面存储了文件的路径和内容，因此获取备份文件的内容和恢复都需要使用到文件的md5值。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在agent通过filebucket客户端获取文件的内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">puppet filebucket get 4894b816078bcca4b63a6feaaf38aff8</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在agent通过filebucket客户端还原文件的内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">puppet filebucket restore /etc/test2 4894b816078bcca4b63a6feaaf38aff8</div></pre></td></tr></table></figure>
<ol>
<li>将master上文件目录下的一个文件，复制到agent的指定位置，如果文件存在就先对文件进行备份然后覆盖。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置代码如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">node <span class="string">'node1.zhang.com'</span>&#123;</div><div class="line">         file &#123;<span class="string">"/etc/test1"</span>:</div><div class="line">         <span class="built_in">source</span> =&gt; <span class="string">"puppet://puppet.zhang.com/files/test1"</span>,</div><div class="line">         backup =&gt; <span class="string">".bak_<span class="variable">$uptime_seconds</span>"</span>,</div><div class="line">&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ol>
<li>将 agent 上的一个文件复制成另外一个文件，如果目的文件存在就先做备份再覆盖，并制定文件属master和权限。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置代码如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">node <span class="string">'node1.zhang.com'</span>&#123;</div><div class="line">        file &#123;<span class="string">"/tmp/test2"</span>:</div><div class="line">        <span class="built_in">source</span> =&gt; <span class="string">"/etc/passwd"</span>,</div><div class="line">        backup =&gt; <span class="string">".bak_<span class="variable">$uptime_seconds</span>"</span>,</div><div class="line">        group =&gt; nobody,</div><div class="line">        owner =&gt; nobody,</div><div class="line">        mode =&gt; 600;</div><div class="line">&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ol>
<li>在agent上创建/tmp/test3，并制定文件的内容为test3。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置代码如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">node <span class="string">'node1.zhang.com'</span>&#123;</div><div class="line">        file &#123;<span class="string">"/tmp/test3"</span>:</div><div class="line">        content =&gt; <span class="string">"test3"</span>&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ol>
<li>对agent上的某个文件或者文件夹建立软连接。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">node <span class="string">'node1.zhang.com'</span>&#123;</div><div class="line">file &#123;<span class="string">"/tmp/test4"</span>:</div><div class="line">        ensure =&gt; link,</div><div class="line">        target =&gt; <span class="string">"/var/log/messages"</span>;</div><div class="line">        <span class="string">"/tmp/test5"</span>:</div><div class="line">        ensure =&gt; link,</div><div class="line">        target =&gt; <span class="string">"/var/log"</span>;</div><div class="line">        &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以简写成如下代码：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">node <span class="string">'node1.zhang.com'</span>&#123;</div><div class="line">file &#123;<span class="string">"/tmp/test4"</span>:</div><div class="line">        ensure =&gt; <span class="string">"/var/log/messages"</span>;</div><div class="line">        <span class="string">"/tmp/test5"</span>:</div><div class="line">        ensure =&gt; <span class="string">"/var/log"</span>;</div><div class="line">        &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ol>
<li>如果agent上/tmp/test6文件存在的话就删除掉agent上的/tmp/test6文件。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">node <span class="string">'node1.zhang.com'</span>&#123;</div><div class="line">file &#123;<span class="string">"/tmp/test6"</span>:</div><div class="line">        ensure =&gt; absent;</div><div class="line">        &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ol>
<li>如果agent上不存在/tmp/test7文件就在agent上创建/tmp/test6文件。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">node <span class="string">'node1.zhang.com'</span>&#123;</div><div class="line">file &#123;<span class="string">"/tmp/test7"</span>:</div><div class="line">        ensure =&gt; present;</div><div class="line">        &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ol>
<li>在agent上创建目录。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">node <span class="string">'node1.zhang.com'</span>&#123;</div><div class="line">file &#123;</div><div class="line">      <span class="string">"/tmp/test8"</span>:</div><div class="line">      ensure =&gt; directory;</div><div class="line">      &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ol>
<li>删除agent上面的目录。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">node <span class="string">'node1.zhang.com'</span>&#123;</div><div class="line">file &#123; <span class="string">"/tmp/test9/"</span>:</div><div class="line">       ensure =&gt; absent,</div><div class="line">       force =&gt; <span class="literal">true</span>;</div><div class="line">       &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ol>
<li>类似rsync，将master上面的文件目录同步到agent上，不同步含有log的文件和目录，如果含有子目录，递归到子目录，删除master上没有agent上有的文件或者目录。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">node <span class="string">'node1.zhang.com'</span>&#123;</div><div class="line">file &#123;<span class="string">"/tmp/test10"</span>:</div><div class="line">        ensure =&gt; directory,</div><div class="line">        <span class="built_in">source</span> =&gt; <span class="string">"puppet://puppet.zhang.com/files/"</span>,</div><div class="line">        ignore =&gt; <span class="string">'*log*'</span>,</div><div class="line">        recurse =&gt; <span class="literal">true</span>,</div><div class="line">        purge =&gt; <span class="literal">true</span>,</div><div class="line">        force =&gt; <span class="literal">true</span>,</div><div class="line">     &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="四、参考文档"><a href="#四、参考文档" class="headerlink" title="四、参考文档"></a>四、参考文档</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<a href="http://docs.puppetlabs.com/references/stable/configuration.html" target="_blank" rel="external">http://docs.puppetlabs.com/references/stable/configuration.html</a></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Puppet/2. Puppet学习之文件管理/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
